import React, { useEffect, useState } from 'react'
import { View,Image } from '@tarojs/components'
import Taro, { useDidShow,useShareAppMessage } from '@tarojs/taro'

import './index.scss'

export default function FansBase() {
  const [info,setInfo] = useState({})
  const [banner,setBanner] = useState([])
  useEffect(()=>{
    const {router} = Taro.getCurrentInstance()
    Taro.$get(`/act/index/selectAdvertisingListByAlias/_xjtfans`).then(res=>{
      setBanner(res.content.advertisesList[0].imageUrl || '')
    })
    Taro.showLoading({title:'加载中...'})
    Taro.$get('/act/home/newfans','','1').then(res=>{
      const list = res.content.qrcodes || []
      const location = router.params.cityCode || Taro.getStorageSync('cityInfo').cityCode
      if(router.params.cityCode){
        Taro.removeStorageSync('fans-base')
      }
      if(location){
        list.map(res=>{
          if(res.id == location){
            setInfo(res)
            Taro.setStorageSync('fans-base',res)
          }
        })
      }
    })
  },[])
  useDidShow(()=>{
    setInfo([])
    if(Taro.getStorageSync('fans-base')){
      setTimeout(()=>{
        setInfo(Taro.getStorageSync('fans-base'))
      },500)
    }
  })
  useShareAppMessage(()=>{
    return {
      title:'加入联盟观赛群',
      path: `/pages/other/fans-base/index`
    }
  })
  
  function handleJoin(){
    if(process.env.TARO_ENV == 'h5'){
      Taro.$toast('该功能仅支持小程序')
    }
  }
  return (
    <View className="fans-base">
      <Image className="base-back" onClick={()=>{
        Taro.navigateBack().catch(()=>{
          Taro.reLaunch({
            url:'/pages/index/index'
          })
        })
      }} src={require('@/static/返回.svg')} mode="widthFix"></Image>
      {banner?<Image src={banner} mode="widthFix" style="width: 100%; display: block;"></Image>:<Image src={'@/static/gray.jpg'} style="width: 100%; height: 350rpx;"></Image>}
      <View className="base-main">
        <View className="base-item">
          <View className='h2' onClick={()=>{handleJoin()}}>选择要加入的群</View>
          <View className="box-flex" onClick={()=>{
            Taro.navigateTo({
              url:'/pages/other/fans-list/index'
            })
          }}>
            <View className='h3' style={{color:info.name?'':'#90989F'}}>{info.name || '请先选择要加入的群'}</View>
            <View className="at-icon at-icon-chevron-right" style="margin-left: auto;color:#90989F;font-size:16px"></View>
          </View>
        </View>
        {info.name?
        <View className="base-item">
        <hr style="border: 0;height: 1px;border-top: 1px solid #60686F; margin: 64rpx 0" />
          <View className='h2'>点击加入群聊</View>
          <View className="box-flex">
            <Image src={require('@/static/joinchat.svg')} className="item-icon"></Image>
            <View className='h3'>加入群聊</View>
            <View className="at-icon at-icon-chevron-right" style="margin-left: auto;color:#90989F;font-size:16px"></View>
            {info.url?<cell className="item-join" style={{position:'absolute',left:0,right:0,top:0,bottom:0,opacity:0,zIndex:1000}} url={info.url}>加入群聊</cell>:null}
          </View>
        </View>:null}
      </View>
    </View>
  )
}
